package com.wwj.springboot.kafka;

import com.alibaba.fastjson.JSONObject;
import com.wwj.springboot.service.ProductService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;

import java.util.Map;


/**
 * @author wwj
 */
@Configuration
public class MyConsumer {

    @Autowired
    private ProductService productService;


    @KafkaListener(topics = "topicqiang",groupId = "g1")
    public void receiver(ConsumerRecord<String,String> record){
        System.out.println(record.partition() +"\t" + record.value());

        String value = record.value();
        Map<String,Integer> map = JSONObject.parseObject(value,Map.class);
        int  pid = map.get("pid");
        int  userid = map.get("userid");
        productService.updateProduct(pid,userid);
    }

}
